package convertdemo01;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/*
JDK 8之后的转换方式
  DateTimeFormatter完成转换:
  public static DateTimeFormatter ofPattern(String pattern):指定日期格式(模式)

  1.将日期对象转换为日期字符串:TemporalAccessor接口被 LocalDate类,LocalTime,LocalDateTime类实现
     public String format(TemporalAccessor temporal)

  2.将日期字符串转换为日期对象
     在LocalDate类,LocalTime,LocalDateTime类均有
       public static LocalDate parse(CharSequence text)
      ** public static LocalDate parse(CharSequence text, DateTimeFormatter formatter)
*/

public class ConvertDemo02 {
    public static void main(String[] args) {
        //method01();
        //method02();
    }

    //将日期字符串转化为日期对象
    private static void method02() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse("2013-12-02", df);
        System.out.println(localDate.getYear());
    }

    //将日期对象转化为日期字符串
    private static void method01() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = df.format(now);
        System.out.println(format);
    }
}
